package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.entity.CaClassrooms;
import com.dingreading.cloud.soms.entity.table.CaClassroomsTableDef;
import com.dingreading.cloud.soms.mapper.CaClassroomsMapper;
import com.dingreading.cloud.soms.service.CaClassroomsService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaClassroomsServiceImpl extends BaseServiceImpl<CaClassroomsMapper, CaClassrooms, CaClassroomsTableDef> implements CaClassroomsService {

    @Override
    protected CaClassroomsTableDef getTable() {
        return CaClassroomsTableDef.caClassrooms;
    }

    @Resource
    CaClassroomsMapper classroomsMapper;

    @Override
    public Page<CaClassrooms> pageList(PageUtil pageUtil, String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.roomName.like(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<CaClassrooms> getByStoreUid(String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = table.storeUid.eq(storeUid);
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.roomName.like(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public CaClassrooms getBean(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean haveName(String storeUid, Long id, String name) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.roomName.eq(name));
        if (null != id)
            condition.and(table.id.ne(id));

        return exists(condition);
    }

    @Override
    public int dropBean(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        return getMapper().deleteByCondition(condition);
    }

    @Override
    public int updateEnabled(String storeUid, Long id, Integer enabled, String staffUid, String staffName, Date time) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        CaClassrooms data = UpdateEntity.of(CaClassrooms.class);
        UpdateWrapper<CaClassrooms> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        return getMapper().updateByCondition(data, condition);
    }

    @Override
    public List<CaClassrooms> getByStoreUidOrderTopRoomId(String storeUid, Long orderByRoomId) {
        return classroomsMapper.getByStoreUidOrderTopRoomId(storeUid, orderByRoomId);
    }

    @Override
    public void updateUid(Long id, String uid) {
        UpdateChain.of(CaClassrooms.class)
                .set(table.uid, uid)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<CaClassrooms> listByStoreUidAndRoomNameIn(String storeUid, List<String> roomNameList) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid).and(table.roomName.in(roomNameList)));
    }

    @Override
    public List<CaClassrooms> listByStoreUidAndRoomUidIn(String storeUid, List<String> roomUidList) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid).and(table.uid.in(roomUidList)));
    }

}
